package org.etom.impl.ui.wizards;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * The abstract class initializes the GUI for the given wizard page.
 */
public abstract class AbstractNewAccountWizardPage1 extends WizardPage {

  private static final int LAYOUT_SPACING = 10;
  private static final String PAGE_NAME = "NewAccountWizardPage1"; //$NON-NLS-1$
  
  private Composite composite;
  protected Text moodleUrlText;
  protected Text rpcServerUrlText;
  protected Text usernameText;
  protected Text passwordText;
  protected Button validateOnFinish;
  
  protected AbstractNewAccountWizardPage1() {
    super(PAGE_NAME);
  }

  /*===[WizardPage methods][Start]===*/
  @Override
  public void createControl(Composite parent) {
    initComposite(parent);
    addLocation();
    addAuthentication();
    addValidation();
    initPage();
  }
  /*===[WizardPage methods][End]===*/
  
  private void initComposite(Composite parent) {
    this.composite = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(1, false);
    layout.marginHeight = LAYOUT_SPACING;
    layout.marginWidth = LAYOUT_SPACING;
    layout.verticalSpacing = LAYOUT_SPACING;
    this.composite.setLayout(layout);
  }
  
  private void addLocation() {
    Group locationGroup = createGroup(
        WizardsMessages.NewAccount_Wizard_Location_Group);
    
    createLabel(locationGroup,
        WizardsMessages.NewAccount_Wizard_Moodle_Url_Label);
    
    moodleUrlText = createText(locationGroup);
    moodleUrlText.setText(
        WizardsMessages.NewAccount_Wizard_Moodle_Url_Default_Val);
    
    createLabel(locationGroup,
        WizardsMessages.NewAccount_Wizard_RPC_Server_Url_Label);
    
    rpcServerUrlText = createText(locationGroup);
    rpcServerUrlText.setText(
        WizardsMessages.NewAccount_Wizard_RPC_Server_Url_Default_Val);
  }
  
  private void addAuthentication() {
    Group authenticationGroup = createGroup(
        WizardsMessages.NewAccount_Wizard_Authentication_Group);
    
    createLabel(authenticationGroup,
        WizardsMessages.NewAccount_Wizard_Authentication_Username_Label);
    
    usernameText = createText(authenticationGroup);
    
    createLabel(authenticationGroup,
        WizardsMessages.NewAccount_Wizard_Authentication_Password_Label);
    
    passwordText = createText(authenticationGroup, SWT.PASSWORD);
  }
  
  private void addValidation() {
    validateOnFinish = new Button(this.composite, SWT.CHECK);
    validateOnFinish.setText(WizardsMessages.NewAccount_Wizard_Validate_Button);
  }
  
  private void initPage() {
    setControl(this.composite);
    setTitle(WizardsMessages.NewAccount_Wizard_Title);
    setDescription(WizardsMessages.NewAccount_Wizard_Description);
  }
  
  private Group createGroup(final String text) {
    Group group = new Group(this.composite, SWT.NONE);
    group.setLayout(new GridLayout(2, false));
    group.setText(text);
    GridData gridData = new GridData();
    gridData.grabExcessHorizontalSpace = true;
    gridData.horizontalAlignment = SWT.FILL;
    group.setLayoutData(gridData);
    return group;
  }
  
  private static Label createLabel(final Composite parent, final String text) {
    Label label = new Label(parent, SWT.NONE);
    label.setText(text);
    return label;
  }
  
  private static Text createText(final Composite parent) {
    return createText(parent, SWT.NONE);
  }
  
  private static Text createText(final Composite parent, final int style) {
    Text text = new Text(parent, SWT.BORDER | style);
    GridData gridData = new GridData();
    gridData.grabExcessHorizontalSpace = true;
    gridData.horizontalAlignment = SWT.FILL;
    text.setLayoutData(gridData);
    return text;
  }
  
}
